package cn.hznc.controller;

import cn.hznc.domain.entity.RegisterEntity;
import cn.hznc.domain.entity.RolesEntity;
import cn.hznc.domain.entity.UserEntity;

import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.UserException;
import cn.hznc.controller.service.UserService;
import cn.hznc.controller.task.InventoryTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/employee")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private InventoryTask inventoryTask;

    /**
     * 登录操作
     * @param
     * @return
     */
    @RequestMapping(value = "/login" ,method = RequestMethod.POST)
    public ResponseBody login(@RequestBody UserEntity user , HttpServletRequest httpServletRequest , HttpServletResponse response){
        try {
             //userService.login(user.getUsername(),user.getPassword(),httpServletRequest);
             return  ResponseBody.success(userService.login(user.getUsername(),user.getPassword(),httpServletRequest,response));
        }catch (UserException ex){
             return ResponseBody.error(ex.getCode(),ex.getMsg());
        }
    }
    /**
     * 注册操作
     *@param
     * @return
     */
    @RequestMapping(value = "/register" ,method = RequestMethod.POST)
    public ResponseBody  register(@RequestBody RegisterEntity user){

       try{
           userService.register(user);
           return ResponseBody.success();
       }catch (UserException ex){
           return ResponseBody.error(ex.getCode(),ex.getMsg());
       }
    }


    @RequestMapping(value = "/lgout" ,method = RequestMethod.POST)
    public ResponseBody  lgout (HttpServletRequest request){
            request.getSession().removeAttribute("employee");
//            SystemInit.user.setUsername("");
//            SystemInit.user.setPassword("");
//            SystemInit.user.setToken("");
//            SystemInit.user.setId(0);
            return  ResponseBody.success("退出成功");
    }



    @RequestMapping(value ="/login2" , method = RequestMethod.POST)
    public ResponseBody login2 (@RequestBody UserEntity userEntity , HttpServletRequest request) throws UserException {

        try {
            return ResponseBody.success(userService.showDynamicMenu(userEntity , request));
        } catch (UserException ex) {
            return ResponseBody.error(ex.getCode(), ex.getMsg());
        }
    }
    @RequestMapping(value ="/selectRoles" , method = RequestMethod.GET)
    public ResponseBody <List<RolesEntity>> selectRoles (@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        List<RolesEntity> rolesEntityList   =  userService.selectRoles(pageNum,pageSize);
        return  ResponseBody.success(rolesEntityList);
    }

    @RequestMapping(value ="/getEmployeeNameByUserName" , method = RequestMethod.GET)
    public ResponseBody getEmployeeNameByUserName (@RequestParam("userId") Integer userId){
        return  ResponseBody.success(userService.getEmployeeNameByUserName(userId));
    }


}
